import Vue from 'vue'
import VueRouter from 'vue-router'

import Home from 'views/Home'
import AskQuestion from 'views/AskQuestion'
import FindMate from 'views/FindMate'
import Contest from 'views/Contest'
import Search from 'views/Search'
import QuestionSet from 'views/QuestionSet'
import Administrator from 'views/Administrator'
import User from 'views/User'
import Answersheet from 'views/Answersheet'

import Mymatch from 'components/content/user/Mymatch'
import Myquestion from 'components/content/user/Myquestion'
import PersonalSetting from 'components/content/user/PersonalSetting'
import PersonalHome from 'components/content/user/PersonalHome'
import ManageThread from 'components/content/administrator/ManageThread'
import ManageSection from 'components/content/administrator/ManageSection'
import Login from 'components/content/login/Login'
import Register from 'components/content/login/Register'
import Security from 'components/content/user/Security'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    component: Home, // (resolve) => require(['views/Home'], resolve),
    meta: {
      title: '首页',
      requireAuth: false // 添加该字段，表示进入这个路由是需要登录的
    }
  },
  {
    path: '/login',
    component: Login, // (resolve) => require(['components/content/login/Login'], resolve),
    meta: {
      title: '登录',
      requireAuth: false // 添加该字段，表示进入这个路由是需要登录的
    }
  },
  {
    path: '/register',
    component: Register,
    meta: {
      title: '注册',
      requireAuth: false // 添加该字段，表示进入这个路由是需要登录的
    }
  },
  {
    path: '/ask_question',
    component: AskQuestion, // (resolve) => require(['views/AskQuestion'], resolve),
    meta: {
      title: '提问',
      requireAuth: true // 添加该字段，表示进入这个路由是需要登录的
    }
  },
  {
    path: '/find_mate',
    component: FindMate, // (resolve) => require(['views/FindMate'], resolve),
    meta: {
      title: '寻友',
      requireAuth: true // 添加该字段，表示进入这个路由是需要登录的
    }
  },
  {
    path: '/search',
    component: Search, // (resolve) => require(['views/Search'], resolve),
    meta: {
      title: '搜题',
      requireAuth: true // 添加该字段，表示进入这个路由是需要登录的
    }
  },
  {
    path: '/question_set',
    component: QuestionSet, // (resolve) => require(['views/QuestionSet'], resolve),
    meta: {
      title: '问题集',
      requireAuth: true // 添加该字段，表示进入这个路由是需要登录的
    }
  },
  {
    path: '/contest',
    component: Contest, // (resolve) => require(['views/Contest'], resolve),
    meta: {
      title: '竞赛',
      requireAuth: true // 添加该字段，表示进入这个路由是需要登录的
    }
  },
  {
    path: '/administrator',
    component: Administrator, // (resolve) => require(['views/Administrator'], resolve),
    children: [
      {
        path: '',
        redirect: 'manage_section'
      },
      {
        path: 'manage_section',
        component: ManageSection, // (resolve) => require(['components/content/administrator/ManageSection'], resolve),
        meta: {
          title: '科目管理',
          requireAuth: true // 添加该字段，表示进入这个路由是需要登录的
        }
      },
      {
        path: 'manage_thread',
        component: ManageThread, // (resolve) => require(['components/content/administrator/ManageThread'], resolve),
        meta: {
          title: '问题管理',
          requireAuth: true // 添加该字段，表示进入这个路由是需要登录的
        }
      }
    ]
  },
  {
    path: '/answer_sheet',
    component: Answersheet, // (resolve) => require(['views/Answersheet'], resolve),
    meta: {
      title: '问题详情',
      requireAuth: true // 添加该字段，表示进入这个路由是需要登录的
    }
  },
  {
    path: '/user',
    component: User, // (resolve) => require(['views/User'], resolve),
    children: [
      {
        path: '',
        redirect: 'personal_home'
      },
      {
        path: 'personal_home',
        component: PersonalHome, // (resolve) => require(['components/content/user/PersonalHome'], resolve),
        meta: {
          title: '个人首页',
          requireAuth: true // 添加该字段，表示进入这个路由是需要登录的
        }
      },
      {
        path: 'personal_setting',
        name: 'PersonalSetting',
        component: PersonalSetting, // (resolve) => require(['components/content/user/PersonalSetting'], resolve),
        meta: {
          title: '个人设置',
          requireAuth: true // 添加该字段，表示进入这个路由是需要登录的
        }
      },
      {
        path: 'myquestion',
        component: Myquestion, // (resolve) => require(['components/content/user/Myquestion'], resolve),
        meta: {
          title: '我的问题',
          requireAuth: true // 添加该字段，表示进入这个路由是需要登录的
        }
      },
      {
        path: 'mymatch',
        component: Mymatch, // (resolve) => require(['components/content/user/Mymatch'], resolve),
        meta: {
          title: '我的比赛',
          requireAuth: true // 添加该字段，表示进入这个路由是需要登录的
        }
      },
      {
        path: 'security',
        component: Security, // (resolve) => require(['components/content/user/Security'], resolve),
        meta: {
          title: '个人安全',
          requireAuth: true // 添加该字段，表示进入这个路由是需要登录的
        }
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由异常错误处理，尝试解析一个异步组件时发生错误，重新渲染目标页面
router.onError((error) => {
  const pattern = /Loading chunk (\d)+ failed/g
  const isChunkLoadFailed = error.message.match(pattern)
  const targetPath = router.history.pending.fullPath
  if (isChunkLoadFailed) {
    router.replace(targetPath)
  }
})

// 前置守卫(guard)
router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
    // console.log('beforeEach获取当前的token是否存在' + sessionStorage.getItem('token'))
    if (sessionStorage.getItem('token')) { // 通过vuexstate获取当前的token是否存在
      next()
    } else {
      next({
        path: '/login',
        query: { redirect: to.fullPath }// 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    }
  } else {
    next()
  }
})
export default router
